package com.recharge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.recharge.common.enums.ErrorCode;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.ResultSupport;
import com.recharge.common.util.BeanUtilsExtends;
import com.recharge.common.util.PasswordUtil;
import com.recharge.domain.UserLoginDO;
import com.recharge.core.entity.UserLoginEntity;
import com.recharge.core.mapper.UserLoginDao;
import com.recharge.service.UserLoginService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Logger;

/**
 * <p>
 * 管理员登陆表 服务实现类
 * </p>
 *
 * @author reagan
 * @since 2020-08-14
 */
@Service
public class UserLoginServiceImpl extends ServiceImpl<UserLoginDao, UserLoginEntity> implements UserLoginService {

    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Override
    public Mono<ResultDO> add(Mono<UserLoginDO> mono) {
        return mono.flatMap(domain -> {
            UserLoginEntity entity = new UserLoginEntity();
            ResultDO result = BeanUtilsExtends.copy(entity, domain);
            if (!result.isSuccess()) {
                logger.info("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
                return Mono.just(result);
            }

            entity.setCreateDate(Calendar.getInstance().getTime());
            entity.setModifiDate(entity.getCreateDate());
            String salt = System.currentTimeMillis() + "";
            String password = PasswordUtil.getPassword(domain.getPassword(), salt);
            entity.setPassword(password);
            entity.setSalt(salt);

            boolean is = save(entity);

            //判断r的值是否小于0.如果小于0 就代表添加失败
            if(!is) {
                result.setSuccess(false);
                result.setErrorCode(ErrorCode.ADD_USER_ERROR);
                logger.info("error code：" + ErrorCode.ADD_USER_ERROR.getCode() + " msg" + ErrorCode.ADD_USER_ERROR.getMsg());
                return Mono.just(result);
            }
            return Mono.just(result);
        });
    }

    public ResultDO getUserByName(String username) {

        ResultDO result = new ResultSupport();

        QueryWrapper<UserLoginEntity> queryWrapper = new QueryWrapper<UserLoginEntity>();
        queryWrapper.like("username", username);

        UserLoginEntity entity = this.getBaseMapper().selectOne(queryWrapper);

        if(entity == null) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.USER_DOES_NOT_EXIST);
            logger.info("error code：" + ErrorCode.USER_DOES_NOT_EXIST.getCode() + " msg" + ErrorCode.USER_DOES_NOT_EXIST.getMsg());
            return result;
        }

        UserLoginDO domain = getDomain(entity);

        if(domain == null) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_COPY_OBJECT_ERROR);
            logger.info("error code：" + ErrorCode.SYSTEM_COPY_OBJECT_ERROR.getCode() + " msg" + ErrorCode.SYSTEM_COPY_OBJECT_ERROR.getMsg());
            return result;
        }

        result.setModel(ResultDO.FIRST_MODEL_KEY, domain);

        return result;
    }

    private UserLoginDO getDomain(UserLoginEntity entity) {
        ResultDO result = new ResultSupport();
        //判断房源对象是否为空，为空就返回 空，不为空就进行下面的代码
        if(entity == null) return null;
        UserLoginDO domain = new UserLoginDO();

        if(!result.isSuccess()) {
            return null;
        }
        domain = BeanUtilsExtends.copyProperties(domain, entity) ? domain : null;

        //判断拷贝数据到Dto对象是否成功，如果成功则返回domian，如果不成功就返回空
        return domain;
    }

    private  List<UserLoginDO> getDomainList(List<UserLoginEntity> list) {
        //创建一个名为dolist的房源do类型的 list集合
        List<UserLoginDO> doList = new ArrayList<UserLoginDO>();

        //判断传入的list的集合的size是否大于0
        if(list.size() > 0) {
            //大于0是，遍历list，创建do类型的对象 调用getdomain方法
            for(UserLoginEntity entity : list) {

                UserLoginDO domain = this.getDomain(entity);

                //判断得到的domain 是否为空 不为空 就往dolist集合里面添加domian 为空就返回空
                if(domain == null) {
                    return null;
                }
                doList.add(domain);
            }
        } else {
            return null;
        }
        return doList;
    }
}
